package com.zaizai.controller;

import java.util.List;

import com.zaizai.domain.vo.OrderVO;
import jakarta.servlet.http.HttpServletResponse;
import com.zaizai.domain.AjaxResult;
import com.zaizai.domain.Order;
import com.zaizai.domain.dto.OrderCommodityDto;
import com.zaizai.service.IOrderService;
import com.zaizai.utils.page.TableDataInfo;
import com.zaizai.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zaizai.domain.AjaxResult.success;

import lombok.extern.slf4j.Slf4j;

/**
 * 订单Controller
 *
 * @author zaiZai
 * @date 2024-10-21
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        log.info("Getting order list with params: {}", order);
        try {
            startPage();
            List<Order> list = orderService.selectOrderList(order);
            log.info("Found {} orders", list.size());
            return getDataTable(list);
        } catch (Exception e) {
            log.error("Error in order list:", e);
            throw e;
        }
    }

    /**
     * 导出订单列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order) {
        List<OrderVO> list = orderService.getOrderVOList(order);
        ExcelUtil<OrderVO> util = new ExcelUtil<OrderVO>(OrderVO.class);
        util.exportEasyExcel(response, list, "订单信息", "商品信息");
    }


    /**
     * 获取订单详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 获取用户的订单列表
     */
    @GetMapping("/roleId")
    public AjaxResult getOrderInfo(Long roleId) {
        Order order = new Order();
        order.setRoleId(roleId);
        return success(orderService.selectOrderList(order));
    }

    /**
     * 新增订单
     */

    @PostMapping
    public AjaxResult add(@RequestBody OrderCommodityDto orderDto) {
        log.info("orderDto, {}", orderDto);
        return success(orderService.insertOrder(orderDto));
    }

    /**
     * 修改订单
     */

    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return success(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return success(orderService.deleteOrderByIds(ids));
    }
}